package br.com.lumis.saas.service.consumerclient.processaction;

import lumis.doui.processaction.ProcessActionHandler;
import lumis.doui.source.Source;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.sendmail.IMailBody;
import lumis.portal.sendmail.ISingleMail;
import lumis.portal.user.UserConfig;
import lumis.util.TextUtil;
import lumis.util.XmlUtil;
import br.com.lumis.saas.SaasRequestParameters;
import br.com.lumis.saas.service.consumerclient.model.Environment;
import br.com.lumis.saas.service.consumerclient.persistence.dao.EnvironmentDAO;

public class SendEmailProcessActionHandler extends ProcessActionHandler<Source> {

	
	public void processAction() throws PortalException {

		String actionType = XmlUtil.readAttributeString(
				SaasRequestParameters.ACTIONTYPE, processActionNode, "");

		if (actionType.equals("sendMailRequestEnvironment"))
			sendMailRequestEnvironment();
		else if (actionType.equals("sendMailRequestStatus"))

			sendMailRequestStatus();

	}

	private void sendMailRequestEnvironment() throws PortalException,
			ManagerException {
		String to = "suporte@lumis.com.br";

		if (to != null) {
			String environment = getParameter("environment", String.class);
			String type = getParameter("type", String.class);
			String description = getParameter("description", String.class) + "";
			if (description == null)
				description = "";
			String observation = getParameter("observation", String.class);
			if (observation == null)
				observation = "";

			UserConfig user = ManagerFactory.getUserManager().get(
					sessionConfig, sessionConfig.getUserId(), transaction);

			ISingleMail singleMail = ManagerFactory.getSendMailManager()
					.createSingleMail();
			singleMail.setSourceComponent("STR_SERVICE_NAME");
			singleMail
					.setSourceComponentStringsUrl("saas/service/request/strings/strings");
			singleMail.setFrom(user.getEmail());
			singleMail.setTo(to);
			singleMail.setSubject(localize("STR_MAIL_SUBJECT"));
			IMailBody mailBody = singleMail.getBody();

			String htmlBody = localize("STR_MAIL_BODY_HTML;"
					+ TextUtil.escapeLocalizationParameter(douiContext
							.getRequest().getServiceInstanceConfig().getName())
					+ ";"
					+ TextUtil.escapeLocalizationParameter(user.getFullName())
					+ ";"
					+ TextUtil.escapeLocalizationParameter(user.getEmail())
					+ ";"
					+ TextUtil
							.escapeLocalizationParameter(localize("STR_MAIL_SUBJECT"))
					+ ";" + TextUtil.escapeLocalizationParameter(environment)
					+ ";" + TextUtil.escapeLocalizationParameter(type) + ";"
					+ TextUtil.escapeLocalizationParameter(description) + ";"
					+ TextUtil.escapeLocalizationParameter(observation));

			mailBody.setHtmlMsg(htmlBody);

			ManagerFactory.getSendMailManager().addMailToSendQueue(
					sessionConfig, singleMail, null, transaction);
		}

		addDefaultResponse();
	}

	private void sendMailRequestStatus() throws PortalException,
			ManagerException {
		String to = "suporte@lumis.com.br";

		if (to != null) {
			String environmentId = getParameter("environmentId", String.class);
			String status = getParameter("status", String.class);
			String statusTo = getParameter("statusTo", String.class) + "";

			String observationStatus = getParameter("observationStatus",
					String.class);
			if (observationStatus == null)
				observationStatus = "";

			if (status.equals(statusTo))
				throw new PortalException("Os status devem ser diferentes.");

			EnvironmentDAO dao = new EnvironmentDAO();
			Environment environment = dao.get(environmentId, transaction);

			UserConfig user = ManagerFactory.getUserManager().get(
					sessionConfig, sessionConfig.getUserId(), transaction);

			ISingleMail singleMail = ManagerFactory.getSendMailManager()
					.createSingleMail();
			singleMail.setSourceComponent("STR_SERVICE_NAME");
			singleMail
					.setSourceComponentStringsUrl("saas/service/request/strings/strings");
			singleMail.setFrom(user.getEmail());
			singleMail.setTo(to);
			singleMail.setSubject(localize("STR_MAIL_SUBJECT_STATUS"));
			IMailBody mailBody = singleMail.getBody();

			String htmlBody = localize("STR_MAIL_BODY_HTML_STATUS;"
					+ TextUtil.escapeLocalizationParameter(douiContext
							.getRequest().getServiceInstanceConfig().getName())
					+ ";"
					+ TextUtil.escapeLocalizationParameter(user.getFullName())
					+ ";"
					+ TextUtil.escapeLocalizationParameter(user.getEmail())
					+ ";"
					+ TextUtil
							.escapeLocalizationParameter(localize("STR_MAIL_SUBJECT_STATUS"))
					+ ";"
					+ TextUtil.escapeLocalizationParameter(environment
							.getName()) + ";"
					+ TextUtil.escapeLocalizationParameter(status) + ";"
					+ TextUtil.escapeLocalizationParameter(statusTo) + ";"
					+ TextUtil.escapeLocalizationParameter(observationStatus));

			mailBody.setHtmlMsg(htmlBody);

			ManagerFactory.getSendMailManager().addMailToSendQueue(
					sessionConfig, singleMail, null, transaction);
		}

		addDefaultResponse();
	}

}
